var homeInfo = null;
var iosInfo = null;
var handsInfo = null;

var Performance = {
    init: function () {
        this.getData(1, 1);
        // this.getData(2, 1);
        // this.getData(3, 1);
    },
    eventLisenter: function () {
        var that = this;
        $(".intenthtml").click(function () {
            window.location.href = $(this).attr("href");
        });
        $(".basehtml").scroll(function (e) {
            var scrollHeight = $(".basehtml")[0].scrollHeight;
            var height = $(window).height();
            var scrollTop = $(".basehtml").scrollTop();

            if ($("#home").hasClass("active")) {
                if (homeInfo.length==10) {
                    if (scrollTop == scrollHeight - height) {
                        that.getData(1, ++homeInfo.pageNo);
                        // $("#home .p").text("正在加载");
                    }
                } else{
                    $("#home .p").text("-没有更多-");
                }
            } else if ($("#ios").hasClass("active")) {
                if (iosInfo.length==10) {
                    if (scrollTop == scrollHeight - height) {
                        that.getData(2, ++iosInfo.pageNo);
                        // $("#ios .p").text("正在加载");
                    }
                } else{
                    $("#ios .p").text("-没有更多-");
                }
            } else if ($("#hands").hasClass("active")) {
                if (handsInfo.length==10) {
                    if (scrollTop == scrollHeight - height) {
                        that.getData(3, ++handsInfo.pageNo);
                        // $("#hands .p").text("正在加载");
                    }
                } else{
                    $("#hands .p").text("-没有更多-");
                }
            }
        });
        $('a[data-toggle="tab"]').on('click', function (e) {
// 获取已激活的标签页的名称
            var activeTab = $(this).attr("href");

            if(activeTab==".home"&&homeInfo==null){
                Performance.getData(1, 1);
            }else if(activeTab==".ios"&&iosInfo==null){
                Performance.getData(2, 1);
            }else if(activeTab==".hands"&&handsInfo==null){
                Performance.getData(3, 1);
            }
        });
    },
    getData: function (type, pageNo, userid) {
        // $(".p").text("正在加载");
        new AjaxRequest({
            type: "post",
            url: apiService.mySpreadOrderList + "?type=" + type + "&userid=" + userid,
            param: JSON.stringify({pageNo: pageNo, limit: 10}),
            callBack: function (res) {
                alert(res.payload.items.length);
                if (res.code == 302) {
                    window.location.href = res.url;
                } else if (res.success) {
                    var data = res.payload.items;
                    if (type == 1) {
                        $(".homeamt").text(res.payload.awardAmt);
                        homeInfo = data;
                        homeInfo.pageNo = pageNo;
                        changelock("#home", homeInfo);
                        for (let i = 0; i < data.length; i++) {
                            buildDom(data[i], "#home");
                        }
                    } else if (type == 2) {
                        $(".iosamt").text(res.payload.awardAmt);
                        iosInfo = data;
                        iosInfo.pageNo = pageNo;
                        changelock("#ios", iosInfo);
                        for (let i = 0; i < data.length; i++) {
                            buildDom(data[i], "#ios");
                        }
                    } else {
                        $(".handsamt").text(res.payload.awardAmt);
                        handsInfo = data;
                        handsInfo.pageNo = pageNo;
                        changelock("#hands", handsInfo);
                        for (let i = 0; i < data.length; i++) {
                            buildDom(data[i], "#hands");
                        }
                    }
                }
            },
            failBack: function () {
                $(".p").text("-没有更多了-");
            }
        });
    }
}

function changelock(id, data) {
    if (data.length==10) {
        $(id + " .p").text("加载更多");
    } else {
        $(id + " .p").text("-没有更多-");
    }
}

function oncilk(dom) {
    window.location.href = $(dom).attr("href");
}

function buildDom(data, id) {
    var dom1 = `<div class="textinfo flexs-pace-between">
                    <div class="la" style="width:45%">
                        <span>用户昵称</span>
                        <span>${data.nickname}</span>`;
    if (data.count > 1) {
        dom1 += `<a href="performanceitem.html?uesrid=${data.userId}">查看全部</a>`;
    }
    dom1 += `</div>
                    <div class="la" style="width:55%">
                        <p>支付时间：${data.dateStr}</p>
                    </div>
                </div>
                <div class="jl-item flexs-pace-between" style="margin-top:0">
                    <div class="flexs-center jl-l">
                        <img src="${data.goodsImg}"/>
                        <div style="margin-left:0.5rem">
                            <p>${data.goodsTitle}</p>
                            <p>用户性质 : ${data.userType}</p>
                            <p>赠送数量 : ${data.quantity}</p>
                        </div>
                    </div>
                    <div class="flexs-center jl-r">
                        <p style="color:#6b6b6b;">+${data.awardAmt}元</p>
                    </div>
                </div>`;
    var dom2 = `<div class="textinfo flexs-pace-between">
                    <div class="la" style="width:45%">
                        <span>用户昵称</span>
                        <span>${data.nickname}</span>`;
    if (data.count > 1) {
        dom2 += `<a href="performanceitem.html?uesrid=${data.userId}">查看全部</a>`;
    }
    dom2 += `</div>
                    <div class="la" style="width:55%">
                        <p>支付时间：${data.dateStr}</p>
                    </div>
                </div>
                <div class="jl-item flexs-pace-between" style="margin-top:0">
                    <div class="flexs-center jl-l">
                        <img src="${data.goodsImg}"/>
                        <div style="margin-left:0.5rem">
                            <p>${data.goodsTitle}</p>
                            <p>数量 : ${data.quantity}</p>
                            <p>金额 : ${data.orderAmt}</p>
                        </div>
                    </div>
                    <div class="flexs-center jl-r">
                        <p style="color:#6b6b6b;">+${data.awardAmt}元</p>
                    </div>
                </div>`;
    alert(data.itemType);
    if (data.itemType == 1) {
        $(id + " .c").append(dom1);
    } else if (data.itemType == 2) {
        $(id + " .c").append(dom2);
    }

}